const app = getApp();
var bdParse = require('../../bdParse/bdParse.js');
Page({

    // 页面的初始数据
    data: {
        id:'',
        current:0,
        ifsave:0,
        title:'',
        pic:'',
        stock:0, //库存
        price:'',
        content:'',
        page:1,
        pagesize:5,
        pinglunlist:[],
        pinglunlen:0,
        iftanchuang:0,
        t_number:1,  //立即购买数量
        detail_list:{}
    },

    // 页面的生命周期函数 – 监听页面加载
    onLoad(res) {
        // console.log(res)
        this.setData({
            id:res.id
        })
        this.getdata(res.id)
        this.getpinlun_max(res.id,this.data.page,100)
    },

    getdata(id){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'goodsDetail',
            data:{
                id,
                mid:app.globalData.userInfo.mid
            },
            success(e){
                console.log(e)
                var contents = e.data.content.replace(/src="/g,'src="http://02.hbcms.eiewz.cn');
                that.setData({
                    title:e.data.title,
                    pic:'http://'+e.data.pic,
                    stock:e.data.stock,
                    price:e.data.price,
                    content:bdParse.bdParse('article', 'html', contents, that, 5),
                    detail_list:e.data,
                    ifsave:e.data.collect
                })
            }
        });
    },

    getpinlun(id,page,pagesize){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'goodsComments',
            data:{
                goods_id:id,
                page,
                pagesize
            },
            success(e){
                console.log(e)
                that.setData({
                    pinglunlist:e.data.data,
                })
            }
        });
    },

    getpinlun_max(id,page,pagesize){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'goodsComments',
            data:{
                goods_id:id,
                page,
                pagesize
            },
            success(e){
                that.setData({
                    pinglunlen:e.data.data.length
                },function setpage(){
                    if(that.selectComponent('#page')==undefined){
                        setTimeout(setpage, 500);
                    }else{
                        that.selectComponent('#page').change_rellist(e.data.data.length,that.data.pagesize)
                    }
                })
            }
        });
    },

    proHandle(e){
        // console.log(e.currentTarget.dataset)
        this.setData({
            current:e.currentTarget.dataset.idx
        })
    },

    saveHandle(){
        swan.request({
            url: app.globalData.urlapi + 'addCollect',
            method:'POST',
            header:{'content-type': 'application/x-www-form-urlencoded'},
            data:{
                goods_id:this.data.detail_list.id,
                mid:app.globalData.userInfo.mid
            },
            success(e){
                console.log(e)
                swan.showToast({
                    title: e.data.msg,
                    duration:'800'
                });
            }
        });
        // console.log(this.data.ifsave)
        this.setData({
           ifsave:!this.data.ifsave 
        })
    },

    getpage(e){
        // console.log(e.detail)
        this.setData({
            page:e.detail
        })
        this.getpinlun(this.data.id,e.detail,this.data.pagesize)
    },

    addcart(){
        const taht = this;
        swan.getStorage({
            key: 'login_data',
            success:res=>{
                // console.log(res)
                swan.request({
                    url: app.globalData.urlapi + 'add2Cart',
                    data:{
                        id:taht.data.id,
                        shop_num:1,
                        mid:app.globalData.userInfo.mid
                    },
                    success(e){
                        if(e.data.code == 3){
                            swan.showToast({
                                title: e.data.msg,
                                icon:'success',
                                duration:'800'
                            });
                        }else{
                            swan.showToast({
                                title: e.data.msg,
                                icon:'loading',
                                duration:'800'
                            });
                        }
                    }
                });
            }
        })
    },

    less(){
        var t_number = this.data.t_number-1;
        if(t_number>0){
            this.setData({
                t_number
            })
        }else{
            swan.showToast({
                title: '不能再减了',
                icon:'none',
                duration:'800'
            });
        }
    },

    add(){
        var t_number = this.data.t_number+1;
        if(t_number<=this.data.stock){
            this.setData({
                t_number
            })
        }else{
            swan.showToast({
                title: '没有库存了',
                icon:'none',
                duration:'800'
            });
        }
    },

    go_xiadan(){
        var list = this.data.detail_list
        var total = this.data.price*this.data.t_number
        var obj = {}
        obj.id = list.id
        obj.price = list.price
        obj.num = this.data.t_number
        obj.name = list.title
        obj.pic = list.pic
        var attr = []
        for(var a of list.attributes){
            var z = {}
            if(a.name == '尺寸'){
                z.size = a.attrs.find(res=>res.goods_detail).name
            }else{
                z.color = a.attrs.find(res=>res.goods_detail).name
            }
            attr.push(z)
        }
        attr.reverse()
        obj.attrs = attr
        console.log(obj,total)
        swan.navigateTo({
            url: '../npay/npay?goods='+JSON.stringify([obj])+'&total='+ total+'&is_buy='+1
        });
    },

    buynow(){
        this.setData({
            iftanchuang:1
        })
    },

    t_close(){
        this.setData({
            iftanchuang:0
        })
    },

    // 页面的生命周期函数 – 监听页面初次渲染完成
    onReady(res) {

    },

    // 页面的生命周期函数 – 监听页面显示
    onShow(res) {

    },

    // 页面的生命周期函数 – 监听页面隐藏
    onHide(res) {

    },

    // 页面的生命周期函数 – 监听页面卸载
    onUnload(res) {

    },

    // 页面的生命周期函数 – 监听页面重启，单击重启按钮时触发
    onForceReLaunch(res) {

    },

    // 页面的事件处理函数 – 监听用户下拉动作
    onPullDownRefresh(res) {

    },

    // 页面的事件处理函数 – 上拉触底事件的处理函数
    onReachBottom(res) {

    },

    // 页面的事件处理函数 – 用户点击右上角转发
    onShareAppMessage(res) {

    },

    // 页面的事件处理函数 – 页面滚动触发事件的处理函数
    onPageScroll(res) {

    },

    // 页面的事件处理函数 – 当前是 tab 页时，点击 tab 时触发
    onTabItemTap(res) {

    }
});